package com.atguigu.gmall.cart.rpc;

import com.atguigu.gmall.cart.entity.CartInfo;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.cart.vo.SuccessCartVo;
import com.atguigu.gmall.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author:lyd
 * @Date:2023/9/1 18:12
 * @Version:1.0
 * @Description
 **/
@RestController
@RequestMapping("/api/inner/cart")
@Slf4j
public class CartRpcController {

    @Autowired
    private CartService cartService;

    /**
     * 向购物车中添加商品
     *
     * @param skuId
     * @param skuNum
     * @return
     */
    @GetMapping("/add/{skuId}/{skuNum}")
    public Result<SuccessCartVo> add(@PathVariable Long skuId,
                                     @PathVariable Integer skuNum) {

        //1、决定此次请求用哪个购物车:用户登录后还是登录前的
        String cartKey = cartService.determineKey();

        //2、给购物车添加商品
        SuccessCartVo cart = cartService.addCart(skuId, skuNum, cartKey);
        return Result.ok(cart);
    }

    /**
     * 删除批量选中的商品
     *
     * @return
     */
    @GetMapping("/delete/checked")
    public Result deleteChecked() {
        String cartKey = cartService.determineKey();
        cartService.deleteChecked(cartKey);
        return Result.ok();

    }

    /**
     * 获取购物车中选中的数据
     * @return
     */
    @GetMapping("/checked")
    public Result<List<CartInfo>> getChecked() {
        String key = cartService.determineKey();
        List<CartInfo> cartInfoList = cartService.getChecked(key);
        return Result.ok(cartInfoList);
    }
}
